###########################################################################
# Lipps & Voeten
# Voting with Putin:
# Gender, LGBT Rights, and Tacit Support for Russia among Europe's Parliamentarians
# Comparative Political Studies

# Replication file for matching analysis-----------------------------------
###########################################################################

rm(list = ls())

packages <- c("fixest", "tidyverse", "corrplot", "psych", "broom", "ggplot2", "ggpubr", "cem")

for (package in packages) {
  if (!requireNamespace(package, quietly = TRUE)) {
    install.packages(package, dependencies = TRUE)
  }
}

#Packages
library(fixest)
library(tidyverse)
library(corrplot)
library(psych)
library(broom)
library(ggplot2)
library(ggpubr)
library(cem)

#load data
votesrussia <- readRDS("PACE_Russia_votes.rds")

#Some data processing to make cem process the data frame correctly

readr::write_excel_csv(as.data.frame(votesrussia), "votesrussia.csv")
votesrussia <- read.csv("votesrussia.csv")

##### MATCHING 1: gender #####

#to account for selection of women into specific parties
#match treatment and control units with automated coarsening
sample0 <- votesrussia %>%
  filter(country!="RU") %>%
  distinct(MP_ID, voteid, .keep_all=T) %>%
  select(MP_ID, female, country, natparty_vpartyname, v2pagender, Equality, voteid) %>% na.omit() %>% ungroup()

#imbalance check
imbalance(group = sample0$female, data = sample0[,c("Equality", "v2pagender")]) #imbalance greater on equality

#match with automated coarsening
mat <- cem(treatment = "female", data=sample0,
           drop = c("country", "MP_ID", "natparty_vpartyname", "voteid"), eval.imbalance = T)
mat

#Appendix Figure C2.1: Matched observations for matching on gender
matchcem <- sample0[mat$matched,]
unmatchcem <- sample0[mat$matched==FALSE,]
matchcem$match <- 1
unmatchcem$match <- 0
matchcem <- rbind(matchcem, unmatchcem)
matchcem <- cbind(matchcem, mat$w)
matchcem$female <- as.factor(matchcem$female)


match1 <- ggplot(matchcem, aes(x=Equality, y=v2pagender, color=female)) +
  geom_text(aes(label=ifelse(female==1,"F", "M"), color=female),size=5, 
            color=ifelse(matchcem$match==0, "lightgrey", ifelse(matchcem$female==1 & matchcem$match==1,"#fde725", "#440154"))) +
  labs(x="", y="Women in Party leadership") +
  theme_bw()+
  theme(axis.text = element_text(size = 16),
        axis.title = element_text(size = 16))


#Appendix Table C1: Model 1

#this is creating a dataframe for the individuals matched on gender
matchedvotes <- votesrussia %>%
  left_join(matchcem[,c("mat$w", "MP_ID", "match", "voteid")], by=c("MP_ID", "voteid")) %>%
  filter(match==1) %>%
  rename(w = "mat$w")

# regression models
modelfem <- as.formula("Y ~   female + v2pagender + Equality| voteid + natparty_vpartyname")
M1 = feols(modelfem, matchedvotes, weights=matchedvotes$w)

#Appendix Figure C1: Pre- and post conservative turn

#Post conservative turn
cutoff_date <- as.Date("2012-02-21")
votesrussia$postpussy <- ifelse(votesrussia$docdate >= cutoff_date, 1, 0)

###create a pre- and post-pussy balanced sample
#pre
samplepre <- votesrussia %>%
  filter(country!="RU" & docdate<cutoff_date) %>%
  select(MP_ID, female, country, natparty_vpartyname, v2pagender, Equality, voteid) %>% na.omit() %>% ungroup()

matpre <- cem(treatment = "female", data=samplepre,
              drop = c("country", "MP_ID", "natparty_vpartyname", "voteid"), eval.imbalance = T)

matchpre <- samplepre[matpre$matched,] %>% bind_cols(matpre$w[matpre$matched==TRUE]) %>% rename(w="...8") %>%
  left_join(votesrussia[,c("Y", "MP_ID", "voteid", "postpussy")], by=c("MP_ID", "voteid"))

#post
samplepost <- votesrussia %>%
  filter(country!="RU" & docdate>=cutoff_date) %>%
  select(MP_ID, female, country, natparty_vpartyname, v2pagender, Equality,  voteid) %>% na.omit() %>% ungroup()

matpost <- cem(treatment = "female", data=samplepost,
               drop = c("country", "MP_ID", "natparty_vpartyname", "voteid"), eval.imbalance = T)

matchpost <- samplepost[matpost$matched,] %>% bind_cols(matpost$w[matpost$matched==TRUE]) %>% rename(w="...8") %>%
  left_join(votesrussia[,c("Y", "MP_ID", "voteid", "postpussy")], by=c("MP_ID", "voteid"))

#regression
Mpre = feols(modelfem, matchpre, weights=matchpre$w)
Mpost = feols(modelfem, matchpost, weights=matchpost$w)
estimates <- tidy(Mpre) %>% bind_rows(tidy(Mpost), .id="source") %>% filter(str_detect(term, "female"))

pdf(file="appendix_figC1.pdf", width=5, height=5)
ggplot(estimates, aes(x=term, y=estimate, color=source, ymin=estimate-(1.96*std.error), ymax=estimate+(1.96*std.error))) +
  geom_errorbar(size=1, width=0.2, position="dodge") +
  geom_point(position = position_dodge2(width=0.2, preserve = "total"), stat="identity", size=2) +
  geom_hline(yintercept=0, color="black") +
  scale_color_manual(name="Pussy Riot Performance", labels= c("Before", "After"), values=c("deeppink4", "deepskyblue4")) +
  labs(x = "", y = "Coefficient Estimate", ) +
  theme_bw() +
  theme(axis.text.x = element_text(size=12), axis.text.y=element_text(size=12), legend.position = "bottom", legend.text = element_text(size=12),
        panel.grid = element_line(colour = 'grey', linetype = 'dashed'), title = element_text(size=14, vjust=0.5))
dev.off()


#### MATCHING 2: signatories ####

#matching to account for self-selection of signatories
sample1 <- votesrussia %>%
  filter(country!="RU") %>%
  distinct(MP_ID, voteid, .keep_all = T) %>%
  select(MP_ID, signat.all, country, natparty_vpartyname, v2pagender, Equality, voteid) %>% na.omit() %>% ungroup()

#imbalance check
imbalance(group = sample1$signat.all, data = sample1[,c("Equality", "v2pagender")])

#match with automated coarsening
matsig <- cem(treatment = "signat.all", data=sample1,
              drop = c("country", "MP_ID", "natparty_vpartyname", "voteid"), eval.imbalance = T)
matsig #more unmatched than with female!

#Appendix Figure C2.2: matched observations for signatories
matchsig <- sample1[matsig$matched,]
unmatchsig <- sample1[matsig$matched==FALSE,]
matchsig$match <- 1
unmatchsig$match <- 0
matchsig <- rbind(matchsig, unmatchsig)
matchsig <- cbind(matchsig, matsig$w)

match2 <- ggplot(matchsig, aes(x=Equality, y=v2pagender, color=signat.all)) +
  geom_text(aes(label=ifelse(signat.all==1,"X", "0"), color=signat.all),size=5, 
            color=ifelse(matchsig$match==0, "lightgrey", ifelse(matchsig$signat.all==1 & matchsig$match==1,"#fde725", "#440154"))) +
  labs(x="Party Ideology: Equality", y="") +
  theme_bw() +
  theme(axis.text = element_text(size = 16),
        axis.title = element_text(size = 16))

#this is creating a var for the matched individuals
matchedsignat <- votesrussia %>%
  left_join(matchsig[,c("matsig$w", "MP_ID", "match", "voteid")], by=c("MP_ID", "voteid")) %>%
  filter(match==1) %>%
  rename(w = "matsig$w")

#Appendix Table C1: Model 2

#regression
modelsig <- as.formula("Y ~ signat.all + v2pagender + Equality | voteid + natparty_vpartyname")
M1s = feols(modelsig, matchedsignat, weights=matchedsignat$w)


#### MATCHING 3: matching on signatories of declarations not targeting Russia

#matching to account for self-selection of signatories
sample2 <- votesrussia %>%
  filter(country!="RU") %>%
  distinct(MP_ID, voteid, .keep_all = T) %>%
  select(MP_ID, signat.notrussia, country, natparty_vpartyname, v2pagender, Equality, voteid) %>% na.omit() %>% ungroup()

#imbalance check
imbalance(group = sample2$signat.notrussia, data = sample2[,c("Equality", "v2pagender")])

#match with automated coarsening
matsig2 <- cem(treatment = "signat.notrussia", data=sample2,
               drop = c("country", "MP_ID", "natparty_vpartyname", "voteid"), eval.imbalance = T)
matsig2 #more unmatched than with female!

#create data frame
matchsig2 <- sample2[matsig2$matched,]
unmatchsig2 <- sample2[matsig2$matched==FALSE,]
matchsig2$match <- 1
unmatchsig2$match <- 0
matchsig2 <- rbind(matchsig2, unmatchsig2)
matchsig2 <- cbind(matchsig2, matsig2$w)

#Appendix Figure C2.3: matched observations for signatories

match3 <- ggplot(matchsig2, aes(x=Equality, y=v2pagender, color=signat.notrussia)) +
  geom_text(aes(label=ifelse(signat.notrussia==1,"X", "0"), color=signat.notrussia),size=5, 
            color=ifelse(matchsig2$match==0, "lightgrey", ifelse(matchsig2$signat.notrussia==1 & matchsig2$match==1,"#fde725", "#440154"))) +
  labs(x="", y="") +
  theme_bw()+
  theme(axis.text = element_text(size = 16),
        axis.title = element_text(size = 16))

matchedsignat2 <- votesrussia %>%
  left_join(matchsig2[,c("matsig2$w", "MP_ID", "match", "voteid")], by=c("MP_ID", "voteid")) %>%
  filter(match==1) %>%
  rename(w = "matsig2$w")

#Appendix Table C1: Model 3

#regression
modelsig2 <- as.formula("Y ~   signat.notrussia + v2pagender + Equality | voteid + natparty_vpartyname")
M1s2 = feols(modelsig2, matchedsignat2, weights=matchedsignat2$w)

##### OUTPUT
#Labels of main variables
dict = c(v2pagender = "Women party leadership", Equality="Cultural Liberalism",
         female="Female", Y = "Pro-Russia vote", voteid = "Vote ID", natparty_vpartyname = "National Party",
          postpussy = "Post Pussy Riot Performance", signat.all = "Signed LGBT Declaration", signat.notrussia = "Signed LGBT Declaration, not Russia")

#Appendix Table C1
etable(list(M1, M1s, M1s2), digits = "r3", dict=dict, 
       file = "TableMatchALL_revision.tex", replace = TRUE, 
       title = "Regression on all votes, all countries, balancing for party level ideology and women leadership. Model 1 matches on gender, Model 2 matches on signatories, and Model 3 matches on signatories of decla-
      ration not targeting Russia")

#Main Figure 6
estimates <- tidy(M1) %>% bind_rows(tidy(M1s), .id="model") %>%
  bind_rows(tidy(M1s2), .id = "model") %>%
  filter(term=="signat.notrussia" | term=="signat.all" | term=="female")

pdf(file="main_fig6.pdf", width=7, height=5)
ggplot(estimates, aes(x=term, y=estimate, ymin=estimate-(1.96*std.error), ymax=estimate+(1.96*std.error))) +
  geom_errorbar(size=1, position="dodge", width=0.3) +
  geom_point(position = position_dodge2(width=0.3, preserve = "total"), stat="identity", size=4) +
  geom_hline(yintercept=0) +
  scale_x_discrete(labels=c("Female", "Signatory", "Signatory, not Russia")) +
  labs(x="", y="Estimate and 95% Conf. Int.") +
  theme_bw() +
  theme(axis.text.x = element_text(size=16), axis.text.y=element_text(size=16), legend.position = "bottom", legend.text = element_text(size=16),
        panel.grid.major = element_line(colour = 'grey', linetype = 'dashed'), title = element_text(size=18, vjust=0.5))
dev.off()

#Appendix Figure C2
pdf(file="appendix_figC2.pdf", width = 12, height=8)
ggarrange(match1, match2, match3, ncol=3, common.legend = T, 
          labels=c("Match: MP gender", "Match: Signed LGBT", "Match: Signed LGBT non-Russia"), label.x=c(0, -0.02, -0.17))
dev.off()
